Xbasic

FOR EACH ... NEXT

Syntax

FOR EACH Placeholder IN List [ statements ] [ CONTINUE ] [ statements ] [ EXIT FOR ] NEXT

Arguments

Placeholder

An arbitrary name. When referring to the placeholder value inside the loop, reference the variable's .value property. See examples below.

List

The name of a CR-LF delimited list, an array, or a collection.

Description

The FOR EACH ... NEXT operators allow you to step through a list, collection, or array without knowing the number of elements that it contains.

Discussion

The FOR EACH ... NEXT operator loops through a list, collection, or array, allowing you iterate over each element. As you iterate through the list, collection, or array the element for the current position in the list is stored in a Placeholder. The Placeholder contains information about the element, including its value. In order to reference the value for the current position in the list, you must use the value property for the place holder. For example, Foo.value.

The example below demonstrates iterating over a CR-LF delimited list containing the letters 'a' through 'e' and displays a message box for each letter, skipping the letter 'c'.

Example

dim letters as C = <<%txt%
a
b
c
d
e
%txt%
     
FOR EACH letter IN letters
    'refer to the placeholder .value property to get the value of the current element in letters.
    if (letter.value = "c") then
        CONTINUE
    end if
    
    ui_msg_box("", letter.value)
NEXT
This example will only run in Desktop applications or the Interactive Window in Alpha Anywhere as the ui_msg_box function only works in Desktop applications.

See Also